import sys
sys.path.append("../")
from frameworks.cores.LoadClass import *
from controller.LoadModelController import *
from controller.PushStockController import *
from model.StockBaseInfo import *
import json

class ApiController:
    def __init__(self):
        self.load = LoadClass()
        self.model = LoadModelController()
        self.baseinfo = StockBaseInfo()

    def getModels(self):
        return self.model.getModelData()

    def getHomePageData(self,args):
        con = PushStockController()
        return con.getStockData(args["code"])

    def getFupan(self):
        args = {}
        args["func"] = "getToday"
        # 获取个股股东信息
        bankuaiRs = self.load.load("controller.BaiKuaiController", "BaiKuaiController", args)
        data = "[{\n"
        data += "        name: '上涨',\n"
        data += "        value: #a1#,\n"
        data += "        children: [#a2#]\n"
        data += "    }, {\n"
        data += "        name: '下跌',\n"
        data += "        value: #a3#,\n"
        data += "        children: [#a4#]\n"
        data += "    }]\n"
        info = {}
        aRed = []
        aBlue = []
        BlueAll = 0
        RedAll = 0
        for row in bankuaiRs:
            realval = float(row["real_value"].replace("%", ""))+20
            option = "{\n"
            option += "name: '" + row["name"] + "',\n"
            option += "value: " + str(realval) + "\n"
            option += "}\n"
            if row["real_value"].find("-") >= 0:
                aBlue.append(option)
                BlueAll += realval
            else:
                aRed.append(option)
                RedAll += realval
        info["data"] = data.replace("#a1#",str(RedAll)).replace("#a2#",",".join(aRed)).replace("#a3#",str(BlueAll)).replace("#a4#",",".join(aBlue))
        print(info)
        return info

    def getAutoSelect(self):
        args = {}
        args["func"] = "getAll"
        # 获取个股股东信息
        return self.load.load("controller.AutoSelectController", "AutoSelectController", args)

    def getZhangTingPage(self,args):
        args["func"] = "getAll"
        # 获取个股股东信息
        return self.load.load("controller.ZhangTingController", "ZhangTingController", args)

    def getGansiduiPage(self,args):
        args["func"] = "getAll"
        # 获取个股股东信息
        return self.load.load("controller.GansiduiController", "GansiduiController", args)

    def getMultiCode(self,code):
        return {"sinacode":self.baseinfo.translateSinaCode(code)}
